<?php
set_time_limit(300);
defined('IN_PHPCMS') or exit('No permission resources.');
// 模型缓存路径
define('CACHE_MODEL_PATH', CACHE_PATH . 'caches_model' . DIRECTORY_SEPARATOR . 'caches_data' . DIRECTORY_SEPARATOR);
// 定义在单独操作内容的时候，同时更新相关栏目页面
define('RELATION_HTML', true);

pc_base::load_app_class('admin', 'admin', 0);
pc_base::load_sys_class('form', '', 0);
pc_base::load_app_func('util');
pc_base::load_sys_class('format', '', 0);

/**
 * APP菜单管理
 */
class appmemu extends admin
{

    private $db,$type;

    public $siteid;

    public function __construct()
    {
        parent::__construct();
        $this->db = pc_base::load_model('app_label_model');
        $this->siteid = get_siteid();
    }

    /**
     * app菜单列表
     */
    public function init()
    {    
        //获取管理员真实姓名数据
        $admin_model = pc_base::load_model('admin_model');
        $adminInfo = $admin_model->getRealnameInfo();

        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;
        $where = ' siteid = '.$this->siteid.' AND status = 1 ';
        if(isset($_GET['title']) && !empty($_GET['title'])) {
            $title = strip_tags(trim($_GET['title']));
            $where .= " AND `name` like '%$title%'";
        }
        if(isset($_GET['user_type']) && !empty($_GET['user_type'])) {
            $where .= " AND `user_type`=".$_GET['user_type'];
        }
        $lists = $this->db->listinfo($where, 'sort DESC,id DESC', $page, 20);
        $pages = $this->db->pages;
        $big_menu = array('javascript:window.top.art.dialog({id:\'add\',iframe:\'?m=admin&c=appmemu&a=add\', title:\''.L('add_app_memu').'\', width:\'580\', height:\'200\', lock:true}, function(){var d = window.top.art.dialog({id:\'add\'}).data.iframe;var form = d.document.getElementById(\'dosubmit\');form.click();return false;}, function(){window.top.art.dialog({id:\'add\'}).close()});void(0);', L('add_app_memu'));
        include $this->admin_tpl('app_list');
    }

    /**
     * 添加app菜单
     */
    public function add ()
    {
        if(isset($_POST['dosubmit']) && ! empty($_POST['dosubmit'])) {
            $app = $this->check($_POST['app']);
            $id = $this->db->insert($app, true);
            if ($id) {
                //记录行为日志
                $log_content = '<span style="color:green;">添加app菜单</span>[菜单id：'.$id.'，菜单名称：'.$app['name'].']';
                $this->manage_admin_log($log_content);
                showmessage(L('operation_success'), '?m=admin&c=appmemu', '', 'add');
            } else {
                showmessage(L('operation_failure'), HTTP_REFERER);
            }
        } else {
            $show_validator = '';
            include $this->admin_tpl('app_add');
        }
    }

    /**
     * app菜单修改
     */
    public function edit ()
    {
        if (! isset($_GET['id']) || empty($_GET['id'])) {
            showmessage(L('illegal_action'), HTTP_REFERER);
        }
        $id = intval($_GET['id']);
        if(isset($_POST['dosubmit']) && ! empty($_POST['dosubmit'])) {
            $app = $this->check($_POST['app']);
            $id = $this->db->update($app, array('id' => $id));
            if ($id) {
                //记录行为日志
                $log_content = '<span style="color:blue;">修改app菜单</span>[菜单id：'.$_GET['id'].'，菜单名称：'.$app['name'].']';
                $this->manage_admin_log($log_content);
                showmessage(L('operation_success'), '?m=admin&c=appmemu', '', 'edit');
            } else {
                showmessage(L('operation_failure'), HTTP_REFERER);
            }
        } else {
            $show_validator = '';
            $info = $this->db->get_one(array('id'=>$id));
            include $this->admin_tpl('app_edit');
        }
    }

    /**
     * app菜单删除
     */
    public function delete ()
    {
        if (! isset($_REQUEST['id']) || empty($_REQUEST['id'])) {
            showmessage(L('illegal_action'), HTTP_REFERER);
        }
        $id = $_REQUEST['id'];
        if (is_array($id)) {
            foreach ($id as $k => $v) {
                if (intval($v)) {
                    $id[$k] = intval($v);
                } else {
                    unset($id[$k]);
                }
            }
            $id = implode('\',\'', $id);
        }
        $result = $this->db->update(array('status'=>2),"id in ('$id')");

        //记录行为日志
        $log_content = '<span style="color:red;">删除app菜单</span>[菜单id：('.$id.')]';
        $this->manage_admin_log($log_content);

        if ($result) {
            showmessage(L('operation_success'), HTTP_REFERER);
        } else {
            showmessage(L('operation_failure'), HTTP_REFERER);
        }
    }

    /**
     * 排序
     */
    public function listorder() {
        if(isset($_GET['dosubmit'])) {
            foreach($_POST['listorders'] as $id => $listorder) {
                $this->db->update(array('sort'=>$listorder), array('id' => $id));
            }
            showmessage(L('operation_success'));
        } else {
            showmessage(L('operation_failure'));
        }
    }

    /**
     * 表单验证
     * 
     * @param array $data
     *            表单传递的值
     * @param string $a
     *            add/edit添加操作时，自动加上默认值
     */
    private function check($data, $a = 'add')
    {
        if ($a == 'add') {
            $data['siteid'] = $this->siteid;
            $data['create_time'] = date('Y-m-d H:i:s');
            $data['created_by'] = $_SESSION['userid'];
            $data['update_time'] = date('Y-m-d H:i:s');
            $data['updated_by'] = $_SESSION['userid'];
        } else {
            $data['update_time'] = date('Y-m-d H:i:s');
            $data['updated_by'] = $_SESSION['userid'];
        }
        return $data;
    }
}